package ysq.xyg.${module}.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.${module}.dao.${entity}Dao;
import ysq.xyg.${module}.model.po.${entity};
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.${module}.model.vo.${entity}Vo;
import ysq.xyg.${module}.service.${entity}Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * ${tableComment}service实现
 *
 * @author ${author}
 * @date ${.now}
 */
@Service("${entity?uncap_first}Service")
public class ${entity}ServiceImpl extends BaseServiceImpl<${entity}> implements ${entity}Service {

    @Resource
    private ${entity}Dao ${entity?uncap_first}Dao;

    private ${entity} ${entity?uncap_first};

    @Autowired
    public void setBaseDao(${entity}Dao ${entity?uncap_first}Dao) {
        this.baseDao = ${entity?uncap_first}Dao;
    }


    /**
     * 分页查询
     *
     * @param ${entity?uncap_first}Vo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author ${author}
     * @date ${.now}
     */
    public ReturnEntity page(${entity}Vo ${entity?uncap_first}Vo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<${entity}> list = acceptList(${entity?uncap_first}Vo);
        PageInfo<${entity}> page=new PageInfo<${entity}>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param ${entity?uncap_first}Vo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author ${author}
     * @date ${.now}
     */
    public ReturnEntity addOrUpdate(${entity}Vo ${entity?uncap_first}Vo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(${entity?uncap_first}Vo.getId())){
            ${entity?uncap_first}=new ${entity}(currentUser);
            isUpate=false;
        }else{
            ${entity?uncap_first}=selectByPrimaryKey(${entity?uncap_first}Vo.getId());
            if(ObjectUtil.isNull(${entity?uncap_first})){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            ${entity?uncap_first}.setUpdateTime(new Date());
            ${entity?uncap_first}.setUpdateUser(currentUser.getId());
        }
        <#list table.columns as column>
        <#if column.pk>
        <#else>
            <#if column.columnName!="RemoveTag" && column.columnName!="CreateUser" && column.columnName!="CreateTime" && column.columnName!="UpdateTime" && column.columnName!="UpdateUser">
        ${entity?uncap_first}.set${column.columnName}(${entity?uncap_first}Vo.get${column.columnName}());
            </#if>
        </#if>
        </#list>
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(${entity?uncap_first});
        }else{
            num=insert(${entity?uncap_first});
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author ${author}
     * @date ${.now}
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        ${entity} ${entity?uncap_first}=selectByPrimaryKey(id);
        if(${entity?uncap_first}!=null){
            ${entity?uncap_first}.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            ${entity?uncap_first}.setUpdateTime(new Date());
            ${entity?uncap_first}.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(${entity?uncap_first});
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param ${entity?uncap_first}Vo 对象
     * @return List<BaseDict> BaseDict集合
     * @author ${author}
     * @date ${.now}
     */
    public List<${entity}> acceptList(${entity}Vo ${entity?uncap_first}Vo){
        Example example = new Example(${entity}.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(${entity?uncap_first}Vo.getRemoveTag())){
            criteria.andEqualTo("removeTag",${entity?uncap_first}Vo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return ${entity?uncap_first}Dao.selectByExample(example);
    }
}
